package com.eclipse.training.ui;

import org.eclipse.jface.viewers.DecorationOverlayIcon;
import org.eclipse.jface.viewers.IDecoration;
import org.eclipse.jface.viewers.ILabelDecorator;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.swt.graphics.Image;

import com.eclipse.training.ImagesRegistry;
import com.eclipse.training.Utils;
import com.eclipse.training.model.Galaxy;
import com.eclipse.training.model.GalaxyCluster;
import com.eclipse.training.model.SolarSystem;
import com.eclipse.training.model.StarCluster;

public class UniverseDecoratorLabelProvider implements ILabelDecorator {

	@Override
	public void addListener(ILabelProviderListener listener) {
	}

	@Override
	public void dispose() {
	}

	@Override
	public boolean isLabelProperty(Object element, String property) {
		return false;
	}

	@Override
	public void removeListener(ILabelProviderListener listener) {
	}

	@Override
	public Image decorateImage(Image image, Object element) {
		if (element instanceof Galaxy) {
			Object[] s = ((Galaxy) element).getStars().toArray();
			Object[] sC = ((Galaxy) element).getStarClusters().toArray();
			Object[] n = ((Galaxy) element).getNebulae().toArray();
			if (Utils.concat(s, Utils.concat(sC, n)).length == 0)
				return decorate(image);
		} else if (element instanceof GalaxyCluster) {
			if (((GalaxyCluster) element).getGalaxies().size() == 0)
				return decorate(image);
		} else if (element instanceof SolarSystem) {
			Object[] a = ((SolarSystem) element).getAsteroids().toArray();
			Object[] p = ((SolarSystem) element).getPlanets().toArray();
			Object[] s = ((SolarSystem) element).getStars().toArray();
			if (Utils.concat(a, Utils.concat(p, s)).length == 0)
				return decorate(image);
		} else if (element instanceof StarCluster) {
			if (((StarCluster) element).getStars().size() == 0)
				return decorate(image);
		}

		return null;
	}

	private Image decorate(Image image) {
		return new DecorationOverlayIcon(image, ImagesRegistry.getInstance()
				.getDescriptor(ImagesRegistry.DECORATOR),
				IDecoration.BOTTOM_LEFT).createImage();
	}

	@Override
	public String decorateText(String text, Object element) {
		return null;
	}

}
